<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="false" title="请假申请" back-icon-color='#333333' height="50" :border-bottom="false">
			<text class="iconfont icon-fanhuijian2" style="font-size:30rpx; margin-left:30rpx; font-weight:bold;"
				@click="previous_pages"></text>
		</u-navbar>
		<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
		<view class="topBox">
			<view v-for="(topItem,topIndex) in quanbu" :key="topIndex" :data-name="topIndex" @click="changeIndex"
				:class="[activeIndex==topIndex?'con':'']">
				<text></text>
				{{topItem}}
			</view>
		</view>
		<view class="affairs">
			<text>{{absence_type}}</text>
			<text @click.stop="show=true"></text>
		</view>
		<view class="list">
			<view class="absence" v-for="(item,index) in absenceList" :key="index">
				<view class="compellation">
					<text>姓名:</text>
					<text>{{item.student_user_info?item.student_user_info.full_name:''}}</text>
					<text :style="{color:$status(item.status,2)}">{{$status(item.status,1)}}</text>
					<text :style="{background:$status(item.status,4)}"
						@click="skip_page(item)">{{$status(item.status,3)}}</text>
				</view>
				<view class="time">
					<view class="top">
						<text></text>
						<text>{{item.startTime}}</text>
					</view>
					<view class="bottom">
						<text></text>
						<text>{{item.endTime}}</text>
					</view>
				</view>
				<view class="reason">
					<text>原因</text>
					<text>{{item.reason_type}}</text>
				</view>
				<view class="detail_reason">
					<text>详细原因</text>
					<text>{{item.reason}}</text>
				</view>
				<view class="accessory">
					<text>附件</text>
					<text>{{item.files}}</text>
				</view>
				<view class="submit">
					<text>提交时间</text>
					<text>{{item.createTime}}</text>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import request from '../../request.js'
	import time from '../../time.js'
	export default {
		data() {
			return {
				total_num: 0, //进行记录全部
				pending_num: 0, //进行记录待审核
				passed_num: 0, //进行记录已通过
				unpassed_num: 0, //进行记录未通过
				status: 'loading',
				page: 0,
				feye: 0,
				last_page: '',
				activeIndex: 0,
				quanbu: ['全部', '待审核', '已通过', '未通过'],
				absence_type: '',
				absence_list: [],
				show: false,
				back_page: 1,
				list: [{
						value: '1',
						label: '事假'
					},
					{
						value: '2',
						label: '全部'
					}
				],
			}

		},
		onLoad(option) {
			if (option.id) {
				switch (option.id) {
					case 1:
						this.back_page = 1;
						break;
					case 2:
						this.back_page = 2;
						break;
					case 3:
						this.back_page = 3;
						break;
				}
			}
			this.leaveLists();
		},
		computed: {
			absenceList() {
				if (this.absence_list) {
					this.absence_list.forEach(item => {
						this.$set(item, 'startTime', time.format(item.start_time));
						this.$set(item, 'endTime', time.format(item.end_time));
						this.$set(item, 'createTime', time.format(item.create_time));
					})
					return this.absence_list;
				} else {
					return [];
				}
			}
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				//这里需要进行处理否则触底时更新的数据,会是当前状态下的所有数据，筛选就没有起到作用
				if (this.total_num > 0 && this.activeIndex == 0) {
					console.log(this.total_num, this.page);
					console.log('触发了吗?')
					this.leaveLists(this.page, this.activeIndex, 1);

				} else if (this.total_num <= 0 && this.activeIndex == 0) {
					console.log(this.total_num, this.page);
					console.log('执行到了这一步吗')
					this.leaveLists(this.page, this.activeIndex);
				}

				if (this.pending_num > 0 && this.activeIndex == 1) {
					this.leaveLists(this.page, this.activeIndex, 1);
				} else if (this.pending_num <= 0 && this.activeIndex == 1) {
					this.leaveLists(this.page, this.activeIndex);
				}

				if (this.passed_num > 0 && this.activeIndex == 2) {
					this.leaveLists(this.page, this.activeIndex, 1);
				} else if (this.passed_num <= 0 && this.activeIndex == 2) {
					this.leaveLists(this.page, this.activeIndex);
				}

				if (this.unpassed_num > 0 && this.activeIndex == 3) {
					this.leaveLists(this.page, this.activeIndex, 1);
				} else if (this.unpassed_num <= 0 && this.activeIndex == 3) {
					this.leaveLists(this.page, this.activeIndex);
				}


			}, 500)
		},
		methods: {
			//对返回上一级页面分情况跳转
			previous_pages() {
				switch (this.back_page) {
					case 1:
						this.$toLink('../career/career', {}, 3)
						break;
					case 2:
						uni.navigateBack({
							delta: 1
						})
						break;
					case 3:
						break;
				}
			},
			changeIndex(e) {
				console.log(e.currentTarget.dataset.name);
				let index = e.currentTarget.dataset.name;
				this.activeIndex = index;
				this.feye = index;
				this.absence_type = '';
				//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
				this.page = 0;
				if (this.total_num > 0) {
					this.total_num = 0;
				} else if (this.pending_num > 0) {
					this.pending_num = 0;
				} else if (this.passed_num > 0) {
					this.passed_num = 0;
				} else if (this.unpassed_num > 0) {
					this.unpassed_num = 0;
				}
				//切换时toggle值传零，进行筛选时才传一

				this.leaveLists(1, index, 0);
			},
			confirm(e) {
				//进行下拉框选择时，判断一下当前处于哪种状态
				switch (this.activeIndex) {
					case 0:
						this.total_num++;
						break;
					case 1:
						this.pending_num++;
						break;
					case 2:
						this.passed_num++;
						break;
					case 3:
						this.unpassed_num++;
						break;
				}
				console.log(e);
				this.absence_type = e[0].label;
				this.page = 0; //每次进行切换时将页数重置为零
				this.leaveLists(1, this.activeIndex, 1);
			},
			skip_page(param) {
				let obj = param;
				let str = param.status;
				let content = this.$status(str, 3);
				switch (content) {
					case '去审核':
						this.$toLink('../audit/audit', {
							student_leave_id: obj.student_leave_id,
							type: obj.reason_type,
							reason: obj.reason,
							student_id: obj.student_user_info ? obj.student_user_info.nation_license : '',
							class: obj.classes_info ? obj.classes_info.classes_name : '',
							files: obj.files,
							start_time: obj.startTime,
							end_time: obj.endTime,
							name: obj.student_user_info ? obj.student_user_info.full_name : '',
						});
						break;
				}
			},
			//获取学生请假列表
			leaveLists(param = 1, index = 0, toggle = 0) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				request({
					url: base_url + 'teacher/student_leave/lists',
					data: {
						status: index,
						reason_type: toggle == 1 ? _this.absence_type : 0,
						page: param,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.absence_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.absence_list = [];
								return;
							};
							//没有切换
							if (param == 1 && toggle == 0) {
								//先将原数组清空
								_this.absence_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.absence_list = [];
								} else {
									_this.absence_list = res.data.data.data;
								}
								console.log(_this.absence_list);
								_this.page = 1;
								if (param >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							} else if (param > 1 && toggle == 0) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.absence_list = [..._this.absence_list, ...res.data.data.data];
								console.log(_this.absence_list);
								if (param >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							//进行切换
							if (param == 1 && toggle == 1) {
								console.log('进来了吗');
								//进行切换时将原数组清空
								_this.absence_list = [];
								_this.page = 1;
								_this.absence_list = res.data.data.data;
								if (param >= _this.last_page) {
									_this.status = 'nomore'
									return
								}

							} else if (param > 1 && toggle == 1) {
								console.log(res.data.data.data);

								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.absence_list = [..._this.absence_list, ...res.data.data.data];
								console.log(_this.absence_list);
								if (param >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}

							console.log(_this.absence_list);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		height: 100vh;
		background-color: #F2F2F2;

		.topBox {
			width: 100%;
			height: 65rpx;
			background: #fff;
			position: relative;
			display: flex;
			justify-content: space-evenly;

			view {
				height: 65rpx;
				line-height: 65rpx;
				width: 120rpx;
				text-align: center;
				font-size: 30rpx;
				color: #333333;

				text {
					display: none;
				}


			}

			.con {
				position: relative;
				height: 65rpx;
				line-height: 65rpx;
				color: #4784FF;
				width: 120rpx;

				text {
					display: block !important;
					position: absolute;
					width: 120rpx;
					height: 4rpx;
					background: #4784FF;
					bottom: 0rpx;
					left: 0rpx;
				}
			}

		}

		.affairs {
			width: 100%;
			height: 89rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;

			text {
				&:nth-child(1) {
					color: #333333;
					font-size: 30rpx;
					width: 90rpx;
					margin-left: 36rpx;
				}

				&:nth-child(2) {
					width: 0rpx;
					margin-top: 20rpx;
					height: 0rpx;
					border-width: 20rpx 20rpx 20rpx 20rpx;
					border-color: #ccc transparent transparent transparent;
					border-style: solid;
				}
			}

		}

		.list {
			width: 100%;
			overflow: hidden;

			.absence {
				width: 710rpx;
				margin-left: 20rpx;
				height: 388rpx;
				margin-top: 20rpx;
				box-shadow: 0px 1px 1px 1px #ccc;
				border-radius: 12rpx;
				background-color: #fff;

				.compellation {
					width: 94%;
					margin-left: 3%;
					height: 80rpx;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					border-bottom: 1px solid #F2F2F2;

					text {
						font-size: 28rpx;

						&:nth-child(1) {
							color: #333333;
						}

						&:nth-child(2) {
							color: #333333;
							margin-left: 15rpx;
							width: 150rpx;
						}

						&:nth-child(3) {
							color: #f8b551;
							width: 310rpx;
						}

						&:nth-child(4) {
							width: 80rpx;
							height: 40rpx;
							border-radius: 8rpx;
							background: #fff;
							text-align: center;
							line-height: 40rpx;
							color: #fff;
							font-size: 20rpx;
							margin-left: 20rpx;

						}
					}

					.passed {
						color: #3ebd13 !important;
					}

					.fail {
						color: #ec654e !important;
					}

					#con {
						width: 80rpx;
						height: 40rpx;
						border-radius: 8rpx;
						background-color: #f8b551;
						text-align: center;
						line-height: 40rpx;
						color: #fff;
						font-size: 20rpx;
						margin-left: 310rpx;
						display: block;
					}
				}

				.time {
					width: 100%;

					.top {
						width: 100%;
						height: 57rpx;
						display: flex;
						justify-content: flex-start;

						text {
							margin-top: 22rpx;
							color: #333333;
							font-size: 28rpx;

							&:nth-child(1) {
								width: 12rpx;
								margin-top: 35rpx;
								height: 12rpx;
								background-color: #ec654e;
								margin-left: 20rpx;
							}

							&:nth-child(2) {
								margin-left: 20rpx;
							}

							&:nth-child(3) {
								margin-left: 20rpx;
							}
						}
					}

					.bottom {
						width: 100%;
						height: 57rpx;
						display: flex;
						justify-content: flex-start;

						text {
							margin-top: 22rpx;
							color: #333333;
							font-size: 28rpx;

							&:nth-child(1) {
								width: 12rpx;
								height: 12rpx;
								margin-top: 35rpx;
								background-color: #abc0f7;
								margin-left: 20rpx;
							}

							&:nth-child(2) {
								margin-left: 20rpx;
							}

							&:nth-child(3) {
								margin-left: 20rpx;
							}
						}
					}
				}

				.reason {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					height: 57rpx;

					text {
						font-size: 24rpx;
						margin-top: 25rpx;

						&:nth-child(1) {
							color: #999999;
							margin-left: 55rpx;
						}

						&:nth-child(2) {
							color: #666666;
							margin-left: 20rpx;
						}
					}
				}

				.detail_reason {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					height: 43rpx;
					overflow: hidden;

					text {
						font-size: 24rpx;
						margin-top: 12rpx;

						&:nth-child(1) {
							color: #999999;
							margin-left: 55rpx;
						}

						&:nth-child(2) {
							color: #666666;
							margin-left: 20rpx;
						}
					}
				}

				.accessory {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					height: 40rpx;

					text {
						font-size: 24rpx;

						&:nth-child(1) {
							color: #999999;
							margin-left: 55rpx;
						}

						&:nth-child(2) {
							color: #666666;
							margin-left: 20rpx;
						}
					}
				}

				.submit {
					width: 100%;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					height: 26rpx;

					text {
						font-size: 24rpx;

						&:nth-child(1) {
							color: #999999;
							margin-left: 55rpx;
						}

						&:nth-child(2) {
							color: #666666;
							margin-left: 20rpx;
						}
					}
				}




				&:nth-child(1) {
					margin-top: 0rpx;
				}
			}
		}

	}
</style>
